/**
 * 高值耗材采购模块，提供医疗服务编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","msgServ","flowInit","datagridServ","panelUtil","deptUserSelect"],
		function($,sysmanMain,msgServ,flowInit,datagridServ,panelUtil,deptUserSelect) {
			
		'use strict';
		var _typeUrl = '/conferenceroomBaseinfo/getList.action',_rootpath,
			el_width = 200,el_big_width=475,window_width=800,window_height=700,
			
			//业务编码
			_bizcode = 'oaConferenceroomApply',
			rootid = 'panel-oa-conferenceroomapply',
			window_id = 'oa-conferenceroomapply-win',
			editIndex
			
		var public_method ={
			
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootid	页面根节点id
			 * @param _options.flowStateSelector	流程状态字段选择器
			 */
			initElement:function(_options){				
				if(window.console){
					console.info(' recipel init loaded and startting ...')
				}
			},
			closeWindow:function(){
				$('#' + rootid +' a.inner-table-btn').unbind('click');
				$('#' + window_id).empty()
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				_moduleConfig.initParam = {editable:true};
				sysmanMain.setCurrentModuConfig(_moduleConfig);
			},
			/**
			 * @param _options.rootid
			 * @param _options.conditionSelector
			 * @param _options.
			 */
			initList:function(_options){
				private_method.initCondition(_options)
			},			
			reCreateField:function(_options){
				private_method.reCreateField(_options)
			},
			rowDblClick:function(index,row){
				private_method.rowDblClick(index,row)
			}
			
		}
		
		var private_method = {
			editCell:function(index,field,value){
				if(window.console)
					console.info(' editCell fired ...')
				var highvalue_inner_table = $('#' + rootid + ' #dg-inner-carapply')
				if(editIndex>-1 && (editIndex!=index)){
					$(highvalue_inner_table).datagrid('acceptChanges');
					$(highvalue_inner_table).datagrid('endEdit',editIndex);							
				}
				
				$(highvalue_inner_table).datagrid('beginEdit',index);
					editIndex = index				
			},
			rowDblClick:function(index,row){
				
				var editWindow = 'oa-conferenceroomapply-win';
				var _param = {}
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				if(!_moduleConfig){
					_moduleConfig={}
				}
				
				var flow_state_el = $('#'+rootid+' form.condition-form #flow_state')
				var flow_state_value = $(flow_state_el).combobox('getValue')
				if(flow_state_value && flow_state_value==='finished'){
					_param.editable = false
				}else{
					_param.editable = true
				}
				_moduleConfig.initParam = _param
				sysmanMain.setCurrentModuConfig(_moduleConfig)
				
				var $grid= $('#'+rootid +" table.dg-list");
				var $edit_window = $('#' + editWindow);
				var _url =  '/'+sysmanMain.getRootPath() +'/oaConferenceroomApply/query/editPage.action';
				var _dgparam = {grid:$grid,window:$edit_window,href:_url,width:window_width,height:window_height,flag:true,existCheckElement:'form'}
								
				_dgparam.onClose=public_method.closeWindow
				
				datagridServ.windowEdit(_dgparam);
			},
			/**
			 * 初始化条件查询框
			 * @param _options.rootid 窗口根节点id
			 * @param _options.conditionSelector 条件框节点选择器
			 */
			initCondition:function(_options){
				if(window.console){
					console.info('recipel init loaded and starting...')
				}
				
				flowInit.initList(_options)
			},
			detailInfo:function(){
				if(window.console){
					console.info('show detail loaded...')
				}
				
				var _config ={}
				//查找耗材清单panel
				var innerpanelElement = $('#'+rootid+' div.inner-table-panel')
				
				var _inpatientno
				if(innerpanelElement.length>0){
					//判断panel状态
					var isopen=panelUtil.getPanelState({target:innerpanelElement})
					if(isopen){
						$(innerpanelElement).panel('close')
					}else{
						$(innerpanelElement).panel('open')
					}
				}
				
			},
			/**
			 * 更新质询子类信息
			 * @param _options.form	
			 * @param _options.record
			 * @param _options.target
			 */
			changeSubItem:function(_options){
				var _subSelect = $(_options.form).find('#inquiry_item_subcode')
				$(_subSelect).combobox({
					textField:'name',
					valueField:'id',
					readonly:false,
					url:_rootpath + '/dictionary/getTree.action?id=' + _options.record.id,
					onSelect:function(record){
						var _item = $(_options.form).find('#inquiry_item_sub')
						$(_item).val(record.text)
					}
				})				
			},
			reCreateField:function(_options){
				
				var normal_width_param = {width:el_width}
				var big_width_param = $.extend({},{width:el_big_width,multiline:true,height:50})
				var $form = _options.form
				_rootpath = '/'+sysmanMain.getRootPath()
				var fConfig = {
					//room_name:$.extend({fieldType:'textbox'},normal_width_param),
					start_room_time:$.extend({fieldType:'datebox'},normal_width_param),
					end_room_time:$.extend({fieldType:'datebox'},normal_width_param),
					room_content:$.extend({fieldType:'textbox'},big_width_param),
					dept_content:$.extend({fieldType:'textbox'},big_width_param),
					room_using:$.extend({fieldType:'textbox'},normal_width_param),
					room_end:$.extend({fieldType:'textbox'},normal_width_param),
					apply_name:$.extend(
						{
							fieldType:'textbox',
							onClickButton:function(data){								
								deptUserSelect.openPage({
									userids:'oa-conferenceroomapply-win #apply_id',
									usernames:'oa-conferenceroomapply-win #apply_name',
									deptid:'oa-conferenceroomapply-win #apply_deptid',
									deptname:'oa-conferenceroomapply-win #apply_deptname'
								})								
							}
						},normal_width_param),
					room_name:$.extend(
							{fieldType:'combobox'},
							{
								textField:'room_name',
								valueField:'room_name',
								url:_rootpath + _typeUrl,
								onSelect:function(record){
									var _item = $(_options.form).find('#id')
									$(_item).val(record.id)
								}
							},normal_width_param)
					
				}
				flowInit.initField({
					fieldConfig:fConfig,
					fieldStatus:_options.data,
					window_id:window_id
				})
			}				
		}	
		
		return public_method;
			
})